package day_8_17;

/**
 * 643. 子数组最大平均数 I
 * https://leetcode.cn/problems/maximum-average-subarray-i/description/
 */
public class Main1 {
    public double findMaxAverage(int[] nums, int k) {
        int n = nums.length;
        int sum = 0;
        int maxSum = Integer.MIN_VALUE; // 求最大这里用最小
        for(int i = 0;i < n;i++) {
            // 1、进窗口
            sum += nums[i];
            // 2、判断是否长度足够
            if(i < k - 1) {
                continue;
            }
            // 3、更新
            maxSum = Math.max(maxSum,sum);
            // 4、出窗口
            sum -= nums[i - (k - 1)];
        }
        return (double) maxSum / k;
    }
}
